﻿using DGSFramework;
using DGSFramework.Models;
using System;
using System.Collections.Generic;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
namespace Framework.Resources
{
    /// <summary>
    /// Summary description for GetRSI
    /// </summary>
    public class GetRSI : IHttpHandler
    {
        DGSFrameworkEntities entity = new DGSFrameworkEntities();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            var status = 0;
            var lstReturn = new List<RSIModel>();
            try
            {
                var dateFrom = DateTime.Parse(HttpContext.Current.Request.Form["dateFrom"].ToString());
                var dateTo = DateTime.Parse(HttpContext.Current.Request.Form["dateTo"].ToString());
                var stockCateID = int.Parse(HttpContext.Current.Request.Form["stockCateID"].ToString());
                var entityConn = entity.Connection as EntityConnection;
                var dbConn = entityConn.StoreConnection as SqlConnection;
                dbConn.Open();
                var sqlQuery = string.Format("EXEC GetRSI @DateFrom =N'{0}' ,@DateTo = N'{1}',@StockID ={2}", dateFrom.ToString("MM/dd/yyyy"), dateTo.ToString("MM/dd/yyyy"), stockCateID);
                var cmd = new SqlCommand(sqlQuery, dbConn);
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var item = new RSIModel();
                        item.StockId = int.Parse(reader["StockId"].ToString());
                        item.StockDataID = int.Parse(reader["StockDataID"].ToString());
                        item.GetTime = DateTime.Parse(reader["GetTime"].ToString());
                        item.GiaThamChieu = float.Parse(reader["GiaThamChieu"].ToString());
                        if (!string.IsNullOrEmpty(reader["Change"].ToString()))
                            item.Change = float.Parse(reader["Change"].ToString());
                        if (!string.IsNullOrEmpty(reader["Gain"].ToString()))
                            item.Gain = float.Parse(reader["Gain"].ToString());
                        if (!string.IsNullOrEmpty(reader["Loss"].ToString()))
                            item.Loss = float.Parse(reader["Loss"].ToString());
                        if (!string.IsNullOrEmpty(reader["AvgGain"].ToString()))
                            item.AvgGain = float.Parse(reader["AvgGain"].ToString());
                        if (!string.IsNullOrEmpty(reader["AvgLoss"].ToString()))
                            item.AvgLoss = float.Parse(reader["AvgLoss"].ToString());
                        if (!string.IsNullOrEmpty(reader["RS"].ToString()))
                            item.RS = float.Parse(reader["RS"].ToString());
                        if (!string.IsNullOrEmpty(reader["RSI"].ToString()))
                            item.RSI = float.Parse(reader["RSI"].ToString());
                        lstReturn.Add(item);
                    }
                }
                dbConn.Close();
                status = 1;
            }
            catch (Exception ex)
            {
                status = -1;
            }
            
            context.Response.Write(
                jsonSerializer.Serialize(
                    new
                    {
                        Status = status,
                        Data = lstReturn
                    }
                )
            );
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}